package com.cdtye.itps.cms.controller.equipment;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.authcore.model.UserSso;
import com.cdtye.common.authcore.util.AuthHelper;
import com.cdtye.common.webcore.util.DateUtil;
import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.itps.cms.dao.equipment.entity.EquipmentDivceAttr;
import com.cdtye.itps.cms.dao.equipment.vo.EquipmentDivceAttrVo;
import com.cdtye.itps.cms.service.equipment.EquipmentDivceAttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.cdtye.common.webcore.web.AjaxJson.error;
import static com.cdtye.common.webcore.web.AjaxJson.success;

@Api("设备属性 前端控制器")
@RestController
@RequestMapping("/equipmentDivceAttr")
public class EquipmentDivceAttrController {
    @Autowired
    private EquipmentDivceAttrService modelService;



    @ApiOperation(value = "保存或更新", notes = "返回对象最新数据")
    @PostMapping(value = "/saveOrUpdate")
    public AjaxJson<EquipmentDivceAttr> saveOrUpdate(@RequestBody EquipmentDivceAttr model){
        UserSso loginUser = AuthHelper.loginUser();
        Date nowDate = DateUtil.getDateTime();

        if(StringUtils.isBlank(model.getId())){
            model.setCreateBy(loginUser.getId());
            model.setCreateDatetime(nowDate);
            model.setUpdateDatetime(nowDate);
        }else{
            EquipmentDivceAttr oldModel = modelService.getById(model.getId());
            if(oldModel==null){
                return error("未查询到修改对象");
            }
        }
        model.setUpdateBy(loginUser.getId());
        model.setUpdateDatetime(nowDate);
        // EquipmentDivceAttr equipmentDivceAttr=modelService.saveOrUpdates(model);
        modelService.saveOrUpdate(model);
        return success(model);
    }

    @ApiOperation(value = "按ID删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @PostMapping(value = "/{id}")
    public AjaxJson deleteById(@PathVariable("id") String id) {
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }
        modelService.removeById(id);
        return success();
    }

    @ApiOperation(value = "统计数量")
    @GetMapping(value = "/geConuts")
    public AjaxJson<Map<String,Object>> geConuts(EquipmentDivceAttrVo vo) throws Exception{
        Map<String, String> count = modelService.count(vo);
        return success(count);
    }



    @ApiOperation(value = "按ID查询")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @GetMapping(value = "/{id}")
    public AjaxJson getById(@PathVariable("id") String id) {
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }
        return success(modelService.getById(id));
    }

    @ApiOperation(value = "列表查询")
    @GetMapping(value = "/list")
    public AjaxJson<List<EquipmentDivceAttr>> list(EquipmentDivceAttrVo vo){
        List<EquipmentDivceAttr> result = modelService.list(vo);
        return success(result);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/listPage")
    public AjaxJson<IPage<Map<String,Object>>> listPage(EquipmentDivceAttrVo vo){
        IPage<Map<String,Object>> result = modelService.listPage(vo);
        return success(result);
    }



    // 获取公共信息部分
    @ApiOperation(value = "状态查询")
    @GetMapping(value = "/getStates")
    public AjaxJson getStates(){
        return success(modelService.getStates());
    }
    // 获取公共信息部分
    @ApiOperation(value = "分类查询")
    @GetMapping(value = "/getTypeList")
    public AjaxJson<List<Map<String,String>>> getTypeList(EquipmentDivceAttrVo vo){
        List<Map<String,Object>> result = modelService.getTypeList(vo);
        return success(result);
    }



}
